package com.example.yrdev.ordering_system.Fragment;

import android.app.Fragment;
import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.CustomView.HorizontalListView;
import com.example.yrdev.ordering_system.Model.CustomViewHolder;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;

/**
 * Created by luke on 12/12/2016.
 */

//消费全部明细
    public  class PurchaseOverallFragment extends Fragment{
        Context context;
        int payModeType=0;

        public PurchaseOverallFragment(){

        }

    public void setPayModeType(int payModeType) {

        this.payModeType = payModeType;

        resetList();


    }
    public  ArrayList<PurchaseReport> getCurrentReport(){
      return   correspondList;

    }
ArrayList<PurchaseReport>correspondList;

    public void setContext(Context context) {
            this.context = context;
        }
        public   CustomAdapter customAdapter;

        public void resetList(){

            try {

                ArrayList<PurchaseReport> arrayList= PurchaseReport.getPurchaseReportBaseOnType(context,payModeType);
                correspondList=arrayList;

                //.getPurchasedHistoryBaseOnPurchaseType(context,payModeType);
                if (arrayList.size()>0){
                    showContent(true);

                    customAdapter=new CustomAdapter(context,arrayList);
                    listView.setAdapter(customAdapter);




                }else {

                    showContent(false);

                }


            }catch (Exception e){


                //GlobalFunction.printTextByTag(e.getMessage());

            }



        }

    private  void showContent(boolean stat){

        if (stat)
        {
            reportContainer.setVisibility(View.VISIBLE);
            noDataView.setVisibility(View.GONE);

        }else {

            noDataView.setVisibility(View.VISIBLE);
            reportContainer.setVisibility(View.GONE);

        }
    }

        HorizontalScrollView reportContainer;
        LinearLayout noDataView;
        ListView listView;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View view=inflater.inflate(R.layout.purchase_commodity_container_1,null);


            reportContainer=(HorizontalScrollView) view.findViewById(R.id.report_container);
            noDataView=(LinearLayout)view.findViewById(R.id.noData);


             listView=(ListView)view.findViewById(R.id.report_list);


            resetList();

            return view;

        }


        public static class CustomAdapter extends BaseAdapter {
            Context context;
            ArrayList <PurchaseReport>arrayList;

            public CustomAdapter(Context context,ArrayList<PurchaseReport> arrayList){
                this.context=context;
                this.arrayList=arrayList;

            }

            public ArrayList<PurchaseReport> getArrayList() {
                return arrayList;
            }

            public void setArrayList(ArrayList<PurchaseReport> arrayList) {
                this.arrayList = arrayList;
                notifyDataSetChanged();
            }

            @Override
            public Object getItem(int i) {
                return arrayList.get(i);

            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public int getCount() {
                return arrayList.size();
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {

                LayoutInflater layoutInflater=LayoutInflater.from(context);

                PurchaseReport goods=arrayList.get(i);

                if (view==null){

                    view=layoutInflater.inflate(R.layout.return_good_table_row,null);
                    CustomViewHolder viewHolder=new CustomViewHolder();

                    viewHolder.setIndex((TextView)view.findViewById(R.id.index));
                    viewHolder.setGoods_ticketID((TextView)view.findViewById(R.id.goods_ticketID));
                    viewHolder.setGoods_code((TextView)view.findViewById(R.id.goods_code));
                    viewHolder.setGoods_name((TextView)view.findViewById(R.id.goods_name));
                    viewHolder.setGoods_purchDate((TextView)view.findViewById(R.id.goods_PurchaseDate));
                    viewHolder.setGoods_amount((TextView)view.findViewById(R.id.goods_amount));
                    viewHolder.setGood_RetailPrice((TextView)view.findViewById(R.id.goods_retail_price));
                    viewHolder.setGood_totalPrice((TextView)view.findViewById(R.id.goods_totalPrice));
                viewHolder.setTick((ImageView)view.findViewById(R.id.tick));

                    //    viewHolder.setGood_PaymentType((TextView)view.findViewById(R.id.goods_paymentType));
                    view.setTag(viewHolder);

                    //    GlobalFunction.printTextByTag("11"+goods.getItem_ID()+" "+goods.getItem_name());
                }else {

                }

                CustomViewHolder viewHolder=(CustomViewHolder)view.getTag();
                if (goods.getUploadMark()==0){
                    viewHolder.getTick().setVisibility(View.GONE);
                }else {
                    viewHolder.getTick().setVisibility(View.VISIBLE);
                }
                viewHolder.getIndex().setText(i+1+"");
                viewHolder.getGoods_ticketID().setText(goods.getTicketID());
                viewHolder.getGoods_code().setText(goods.getCommodityID());
                viewHolder.getGoods_name().setText(goods.getCommodityName());
                viewHolder.getGoods_purchDate().setText(goods.getDate());
                viewHolder.getGoods_amount().setText(String.valueOf(goods.getVolume()));
                viewHolder.getGood_RetailPrice().setText(String.valueOf(goods.getSaleCost())+"元");
                viewHolder.getGood_totalPrice().setText(String.valueOf(goods.getCost())+"元");


                // viewHolder.getGood_PaymentType().setText(goods.getPayModeText());


                return view;
            }
        }
    }


